var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()


Page({
  data: { 
    _longitude: 116.3972282409668, //当前地图位置的中心点(默认定位北京)
    _latitude: 39.90960456049752, //当前地图位置的中心点(默认定位北京)
    hasMore: true,// 判断 数据是否全部加载完
    isLodingLock: true, //锁 防止数据加载
    pageList: [], //分页数据列表
    pageNum: 1, //页码
    isShowKong: false, // 是否是空

    nav: [
      { name: '待巡检', type: 0, num: 0, icon: 'icon-map_1' },
      { name: '已巡检', type: 1, num: 0, icon: 'icon-map_2' }
    ], // tab
    currentTab:0, //
  },
  onLoad: function (options) {
    this.getPagServer(1, true) //获取分页数据
    app._total({ type: 2, is_all: 1 }).then((res) => {
      var nav = this.data.nav
      nav[0].num = res.data.un_deal
      nav[1].num = res.data.to_deal
      this.setData({ nav })
    })
    // this.getLocation();
  },
  onShow: function () {
    this.getLocation();
    // this.getPagServer(1, true) //获取分页数据
  },
getLocation:function(){
  wx.getLocation({
    type: 'wgs84',
    success: (res)=> {
      var latitude = res.latitude
      var longitude = res.longitude
      var speed = res.speed
      var accuracy = res.accuracy
      console.log(latitude+'----'+longitude);
      this.setData({ _latitude: latitude, _longitude: longitude})
      // api.$post(api.location.location,{'longitude':longitude,'latitude':latitude});
    },
    fail:(res) => {
      console.log(res)
      wx.getSetting({
        success: (res) => {
          if (!res.authSetting['scope.userLocation']) {
            wx.hideLoading()
            wx.showModal({
              title: '位置授权失败',
              content: '您需要将小程序的位置信息授权为\n "使用小程序期间和离开小程序后" \n否则将会影响部分功能的正常使用',
              confirmText: '前往授权',
              cancelText:'稍后授权',
              success(res) {
                if (res.confirm) {
                  wx.openSetting({
                    complete: (res) => {
                      wx.getLocation({
                        type: 'gcj02',
                        success: res => {
                          that.getNowSituation(res)
                        }
                      })
                    },
                  })
                } else if (res.cancel) {
                  wx.navigateBack()
                }
              }
            })
          }
        },
      })
    }

  })

},
  onReachBottom() { //滑动到底部触发
    this.loadingMore()
  },
  onPullDownRefresh: function () { // 下拉刷新
    this.getPagServer(1, true) //获取分页数据
  },


  loadingMore: function () {  // 加载更多
    console.log(this.data.isLodingLock, 111)
    if (this.data.isLodingLock) {
      this.setData({
        isLodingLock: false
      })

      this.getPagServer()
    }
  },

  getPagServer(pageNum, isClickTab = false) { //分页  
    var t = this;
    var params = {
      pageNum: pageNum == 1 ? 1 : this.data.pageNum,
      pageSize: 10,
      type: this.data.currentTab,
    }
    if (this.data.city_code) { // 有城市编码获取城市编码
      params = { ...params, city_code: this.data.city_code }
    }
    $.getPageList(api, this, api.pollingManage.getList, params, isClickTab).then((res) => {
      console.log(res,'%%%')
      wx.stopPullDownRefresh() // 接口数据请求完停止刷新
    })
  },
  navTabHandle(e){
    var {index} = $.detail(e)
    this.setData({
      currentTab: index
    })
    this.getPagServer(1, true) //获取分页数据
  },
  toPollingManageTaskDetailsHandle(e){ // 去巡检详情
    var { item } = $.detail(e)
    console.log(item)
    $.linkurl(`/pages/pollingManage/pollingManageTaskDetails/pollingManageTaskDetails?id=${item.id}&station_id=${item.station_id}&ticket_num=${item.ticket_num}`)
  }
})